package com.reptile.util;

import com.alibaba.fastjson.JSON;
import com.google.common.collect.Lists;
import com.reptile.constant.YsReptileConstant;
import com.reptile.dto.NewsDTO;
import com.reptile.enums.YsBaseEnum;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.stereotype.Component;

import java.time.LocalDate;
import java.util.List;

/**
 * 爬取新闻核心类
 *
 * @author songyafei
 */
@Slf4j
@Component
public class NewsDataUtil {


    /**
     * 新浪微博新闻爬取方法
     *
     * @param document
     * @return List<NewsDTO>
     */
    public static List<NewsDTO> XLNewsParseList(Document document) {
        //根据网页标签解析源码
        Elements elements = document.select(".mod06-cont");
        List<NewsDTO> result = Lists.newArrayList();
        Elements elements1 = elements.select("ul").select("li");
        //获取新浪新闻要闻标签数据
        for (Element nextChild : elements1) {
            //获取url标签
            String url = nextChild.select("a[href]").attr("href");
            //获取标题
            String title = nextChild.select("a[href]").text();
            //获取文本
            String content = nextChild.select("a[href]").text();
            NewsDTO newsDTO = NewsDTO.builder()
                    .title(title).url(url).crawlingDate(LocalDate.now())
                    .newsDate(LocalDate.now()).newType(YsBaseEnum.NEWS_TYPE_SX.getCode())
                    .content(content).pictureUrl("")
                    .build();
            result.add(newsDTO);
        }
        return result;
    }


    /**
     * 比特范快讯新闻爬取
     *
     * @param document
     * @return List<NewsDTO>
     */
    public static List<NewsDTO> BTCNewsParseList(Document document) {
        //根据网页标签解析源码
        Elements elements = document.select(".main-content").select("#FlashList");
        List<NewsDTO> result = Lists.newArrayList();
        Elements liElements = elements.select("ul").select("li");
        for (Element nextChild : liElements) {
            //获取url标签
            String url = nextChild.select("a[href]").attr("href");
            //获取文本
            String content = nextChild.select("a[href]").text();
            //获取标题
            String title = nextChild.select("a[href]").text();
            //
            NewsDTO newsDTO = NewsDTO.builder().title(title).url(url)
                    .newsDate(LocalDate.now()).crawlingDate(LocalDate.now())
                    .content(content).newType(YsBaseEnum.NEWS_TYPE_HB.getCode())
                    .build();
            result.add(newsDTO);
        }
        return result;
    }

    /**
     * 百度新闻
     *
     * @param document
     * @return
     */
    public static List<NewsDTO> BaiDuNewsParseList(Document document) {
        //根据网页标签解析源码
        Elements elements = document.select(".bd").select("li");
        List<NewsDTO> result = Lists.newArrayList();
        for (Element item : elements) {
            //获取url标签
            String url = item.select("a[href]").attr("href");
            //获取文本
            String content = item.select("a[href]").select("span").text();
            //获取标题
            String title = item.select("a[href]").text();
            NewsDTO newsDTO = NewsDTO.builder().title(title).url(url)
                    .newsDate(LocalDate.now()).crawlingDate(LocalDate.now())
                    .content(content).newType(YsBaseEnum.NEWS_TYPE_INTERNATIONAL.getCode())
                    .build();
            result.add(newsDTO);
        }
        return result;
    }

    /**
     * 澎湃精选新闻
     *
     * @param document
     * @return
     */
    public static List<NewsDTO> PPaiNewsParseList(Document document) {
        //根据网页标签解析源码
        Elements elements = document.select(".newsbox").select(".news_li");
        List<NewsDTO> result = Lists.newArrayList();
        for (Element item : elements) {
            Elements items = item.select(".news_tu");
            //img
            String imgUrl = items.select("img").attr("src");
            //获取url标签
            String url = items.select(".news_tu").select("a[href]").attr("href");
            //获取文本
            String content = items.select(".news_tu").select("img").attr("alt");
            //获取标题
            String title = items.select(".news_tu").select("img").attr("alt");
            if (StringUtils.isNoneBlank(imgUrl)) {
                NewsDTO newsDTO = NewsDTO.builder().title(title)
                        .url(YsReptileConstant.P_PAI_NEWS_URL + url)
                        .newsDate(LocalDate.now()).crawlingDate(LocalDate.now())
                        .content(content).newType(YsBaseEnum.NEWS_TYPE_XW.getCode())
                        .pictureUrl(imgUrl)
                        .build();
                result.add(newsDTO);
            }
        }
        log.info("澎湃新闻测试{}", JSON.toJSONString(result));
        return result;
    }

    /**
     * 雷锋网业界资讯
     *
     * @param document
     * @return
     */
    public static List<NewsDTO> LeiFIndustryNewsParseList(Document document) {
        //根据网页标签解析源码
        Elements elements = document.select(".list").select(".clr").select("li");
        List<NewsDTO> result = Lists.newArrayList();
        for (Element item : elements) {
            //img
            String imgUrl = item.select(".img").select("img").attr("data-original");
            //获取url标签
            String url = item.select(".word").select("a[href]").attr("href");
            //获取文本
            String content = item.select(".word").select(".des").text();
            //获取标题
            String title = item.select(".word").select("a[href]").attr("title");
            if (StringUtils.isNoneBlank(imgUrl)) {
                NewsDTO newsDTO = NewsDTO.builder().title(title)
                        .url(url)
                        .newsDate(LocalDate.now()).crawlingDate(LocalDate.now())
                        .content(content).newType(YsBaseEnum.NEWS_TYPE_XW.getCode())
                        .pictureUrl(imgUrl)
                        .build();
                result.add(newsDTO);
            }
        }
        return result;
    }
}
